<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<%@ page import="com.geping.etl.utils.EnvUitl" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
    String bxSyn = (String) request.getSession().getAttribute("bxSyn");
    String taiwanUser = (String) request.getSession().getAttribute("taiwanUser");
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <base href="<%=basePath%>">
    <%
        if (taiwanUser == null || !taiwanUser.equals("on")) {
    %>
    <title>部门信息</title>
    <%
    } else {
    %>
    <title>分行信息</title>
    <%
        }
    %>
    <%@include file="head.jsp" %>
</head>
<body>
<table class="easyui-datagrid" fitColumns="true" title="${title}" id="dg1"
       data-options="singleSelect:true,rownumbers:true,toolbar:'#tb',autoRowHeight:false,pagination:true,pageSize:25,pageList:[15,25,35]"
       style="height: 460px">
    <thead>
    <tr>
        <th data-options="field:'id',width:80,align:'center',hidden:true">ID</th>
        <c:if test="${taiwanUser==null||taiwanUser!='on'}">
            <th data-options="field:'departmentId',width:80,align:'center',hidden:true">部门代码</th>
            <th data-options="field:'departmentName',width:80,align:'center'">部门名称</th>
            <th data-options="field:'description',width:80,align:'center'">描述</th>
        </c:if>
        <c:if test="${taiwanUser=='on'}">
            <th data-options="field:'description',width:80,align:'center'">分行名称</th>
            <th data-options="field:'departmentId',width:80,align:'center'">分行号</th>
            <th data-options="field:'departmentName',width:80,align:'center'">部门名称</th>
        </c:if>
    </tr>
    </thead>

    <tbody>
    <c:forEach items="${list}" var="li">
        <tr>
            <td>${li.id}</td>
            <c:if test="${taiwanUser==null||taiwanUser!='on'}">
                <td>${li.departmentId}</td>
                <td>${li.departmentName}</td>
                <td>${li.description}</td>
            </c:if>
            <c:if test="${taiwanUser=='on'}">
                <td>${li.description}</td>
                <td>${li.departmentId}</td>
                <td>${li.departmentName}</td>
            </c:if>
        </tr>
    </c:forEach>
    </tbody>
</table>

<div id="tb" style="padding:5px;height:auto;">

    <%
        if (taiwanUser == null || !taiwanUser.equals("on")) {
    %>
    部门名称:
    <input type="text" style="width:173px;height:21px" id="departmentName" name="departmentName"
           value="${departmentName}">&nbsp;&nbsp;
    <%
    } else {
    %>
    分行名称:
    <input type="text" style="width:173px;height:21px" id="departmentName" name="departmentName"
           value="${departmentName}">&nbsp;&nbsp;
    <%
        }
    %>
    <a class="easyui-linkbutton" iconCls="icon-search" onclick="selectRecordByName()">查询</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <%
        if (bxSyn == null || !bxSyn.equals("on")) {
            if (taiwanUser == null || !taiwanUser.equals("on")) {
    %>
    <a class="easyui-linkbutton" iconCls="icon-add" onclick="addDepartment(1)">新增</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <%
    } else {
    %>
    <a class="easyui-linkbutton" iconCls="icon-add" onclick="addDepartment(2)">新增</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <%
            }
        }
    %>
    <%
        if (bxSyn == null || !bxSyn.equals("on")) {
    %>
    <a class="easyui-linkbutton" iconCls="icon-remove" onclick="remove()">删除</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <%
        }
    %>
    <%
        if (taiwanUser == null || !taiwanUser.equals("on")) {
    %>
    <a class="easyui-linkbutton" iconCls="icon-edit" onclick="editDepartment(1)">修改</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <%
    } else {
    %>
    <a class="easyui-linkbutton" iconCls="icon-edit" onclick="editDepartment(2)">修改</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <%
        }
    %>

    <a class="easyui-linkbutton" iconCls="icon-page-excel" onclick="openExcelTemplet()">导出</a>
</div>


<%
    if (taiwanUser == null || !taiwanUser.equals("on")) {
%>
<div id="addDialog1" class="easyui-dialog" title="新增部门" data-options="iconCls:'icon-save',toolbar:'#tbForAddDialog'"
     style="width:500px;height:260px;top: 266px">
    <!--   新增对话框的工具栏  -->
    <div id="tbForAddDialog1" style="padding-left: 30px;padding-top: 10px">
        <a id="saveButtonId1" class="easyui-linkbutton" data-options="iconCls:'icon-save'"
           onclick="saveForAddDialog(1)">保存</a>&nbsp;&nbsp;
        <a class="easyui-linkbutton" data-options="iconCls:'icon-undo'" onclick="resetForAdd(1)">重置</a>
    </div>

    <!-- 信息录入 -->
    <form id="formForAddDepartment1" method="post">
        <div style="padding-top: 20px">
            <table style="padding-left: 30px">
                <input type="hidden" name="randSession" value="<%=request.getSession().getAttribute("authId")%>"/>
                <tr>
                    <td align="right">部门名称:</td>
                    <td><input class="easyui-validatebox" name="departmentName_add" id="departmentName_add1"
                               data-options="required:true,missingMessage:'部门名称不能为空'"
                               style="width:173px;height: 21px"/>
                    </td>
                </tr>

                <tr>
                    <td align="right">描述:</td>
                    <td><input type="text" name="description_add" id="description_add1"
                               style="height: 100px;width: 300px"/></td>
                </tr>
            </table>
        </div>
    </form>
</div>
</div>

<!-- 修改部门dialog -->
<div style="visibility: hidden;">
    <div id="editDialog1" class="easyui-dialog" title="修改部门"
         data-options="iconCls:'icon-save',toolbar:'#tbForEditDialog'" style="width:500px;height:260px;top: 266px">
        <!--   新增对话框的工具栏  -->
        <div id="tbForEditDialog1" style="padding-left: 30px;padding-top: 10px">
            <a id="editButtonId1" class="easyui-linkbutton" data-options="iconCls:'icon-save'"
               onclick="saveForEditDialog(1)">保存</a>&nbsp;&nbsp;
            <a class="easyui-linkbutton" data-options="iconCls:'icon-undo'" onclick="resetForEdit(1)">重置</a>
        </div>

        <!-- 信息录入 -->
        <form id="formForEditDepartment1" method="post">
            <div style="padding-top: 20px">
                <table style="padding-left: 30px">
                    <input type="hidden" name="randSession" value="<%=request.getSession().getAttribute("authId")%>"/>
                    <input type="hidden" name="editId" id="editId1"/>
                    <input type="hidden" name="isEditDepartmentName" id="isEditDepartmentName1"/>
                    <tr>
                        <td align="right">部门名称:</td>
                        <td><input class="easyui-validatebox" name="departmentName_edit" id="departmentName_edit1"
                                   data-options="required:true,missingMessage:'部门名称不能为空'"
                                   style="height: 21px; width: 173px"/></td>
                    </tr>

                    <tr>
                        <td align="right">描述:</td>
                        <td><input type="text" name="description_edit" id="description_edit1"
                                   style="height: 100px;width: 300px"/></td>
                    </tr>
                </table>
            </div>
        </form>
    </div>
</div>
<%
} else {
%>

<!-- 新增分行dialog -->
<div style="visibility: hidden;">
    <div id="addDialog2" class="easyui-dialog" title="新增分行"
         data-options="iconCls:'icon-save',toolbar:'#tbForAddDialog'"
         style="width:500px;height:260px;top: 266px">
        <!--   新增对话框的工具栏  -->
        <div id="tbForAddDialog2" style="padding-left: 30px;padding-top: 10px">
            <a id="saveButtonId2" class="easyui-linkbutton" data-options="iconCls:'icon-save'"
               onclick="saveForAddDialog(2)">保存</a>&nbsp;&nbsp;
            <a class="easyui-linkbutton" data-options="iconCls:'icon-undo'" onclick="resetForAdd(2)">重置</a>
        </div>

        <!-- 信息录入 -->
        <form id="formForAddDepartment2" method="post">
            <div style="padding-top: 20px">
                <table style="padding-left: 30px">
                    <input type="hidden" name="randSession" value="<%=request.getSession().getAttribute("authId")%>"/>

                    <tr>
                        <td align="right">分行名称:</td>
                        <td><input class="easyui-validatebox" name="description_add" id="description_add2"
                                   data-options="required:true,missingMessage:'分行名称不能为空'"
                                   style="width:173px;height: 21px"/></td>

                    </tr>
                    <tr>
                        <td align="right">分行号:</td>
                        <td><input class="easyui-validatebox" name="departmentId_add" id="departmentId_add2"
                                   data-options="required:true,missingMessage:'分行号不能为空'"
                                   style="width:173px;height: 21px"/></td>
                    </tr>
                    <tr>
                        <td align="right">部门名称:</td>
                        <td><input class="easyui-validatebox" name="departmentName_add" id="departmentName_add2"
                                   data-options="required:true,missingMessage:'部门名称不能为空'"
                                   style="width:173px;height: 21px"/></td>
                    </tr>
                </table>
            </div>
        </form>
    </div>
</div>

<!-- 修改部门dialog -->
<div style="visibility: hidden;">
    <div id="editDialog2" class="easyui-dialog" title="修改分行"
         data-options="iconCls:'icon-save',toolbar:'#tbForEditDialog'" style="width:500px;height:260px;top: 266px">
        <!--   新增对话框的工具栏  -->
        <div id="tbForEditDialog2" style="padding-left: 30px;padding-top: 10px">
            <a id="editButtonId2" class="easyui-linkbutton" data-options="iconCls:'icon-save'"
               onclick="saveForEditDialog(2)">保存</a>&nbsp;&nbsp;
            <a class="easyui-linkbutton" data-options="iconCls:'icon-undo'" onclick="resetForEdit(2)">重置</a>
        </div>

        <!-- 信息录入 -->
        <form id="formForEditDepartment2" method="post">
            <div style="padding-top: 20px">
                <table style="padding-left: 30px">
                    <input type="hidden" name="randSession" value="<%=request.getSession().getAttribute("authId")%>"/>
                    <input type="hidden" name="editId" id="editId2"/>
                    <input type="hidden" name="isEditDepartmentName" id="isEditDepartmentName2"/>
                    <tr>
                        <td align="right">分行名称:</td>
                        <td><input class="easyui-validatebox" name="description_edit" id="description_edit2"
                                   data-options="required:true,missingMessage:'分行名称不能为空'"
                                   style="width:173px;height: 21px"/></td>

                    </tr>
                    <tr>
                        <td align="right">分行号:</td>
                        <td><input class="easyui-validatebox" name="departmentId_edit" id="departmentId_edit2"
                                   data-options="required:true,missingMessage:'分行号不能为空'"
                                   style="width:173px;height: 21px"/></td>
                    </tr>
                    <tr>
                        <td align="right">部门名称:</td>
                        <td><input class="easyui-validatebox" name="departmentName_edit" id="departmentName_edit2"
                                   data-options="required:true,missingMessage:'部门名称不能为空'"
                                   style="width:173px;height: 21px"/></td>
                    </tr>
                </table>
            </div>
        </form>
    </div>
</div>

<%
    }
%>


<!-- 导出模板选择 -->
<div style="visibility: hidden;">
    <div id="selectExcelTemplet" class="easyui-dialog" title="选择导出Excel模板"
         style="width:300px;height:120px;align-items: center;text-align: center;padding: 20px 5px">
        <a class="easyui-linkbutton" iconCls="icon-page-excel" onclick="exportExcel('xls')">xls(2003版)</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <a class="easyui-linkbutton" iconCls="icon-page-excel" onclick="exportExcel('xlsx')">xlsx(2007版)</a>
    </div>
</div>
<!--------------------------------------------------js ------------------------------------------------------------------------------->
<script type="text/javascript">

    $(function () {
        $('#addDialog1').dialog('close');
        $('#editDialog1').dialog('close');
        $('#addDialog2').dialog('close');
        $('#editDialog2').dialog('close');
        $('#selectExcelTemplet').dialog('close');
        $('#dg1').datagrid({loadFilter: pagerFilter, fit: true});//.datagrid('loadData', getData());
        $('.datagrid-header-check').find('input').attr('style', 'display:none');
        $('#dg1').datagrid('getPager').pagination({ //设置分页功能栏
            //分页功能可以通过Pagination的事件调用后台分页功能来实现
            onRefresh: function () {
                selectRecordByName();
            }
        });

    });


    function pagerFilter(data) {
        if (typeof data.length == 'number' && typeof data.splice == 'function') {	// is array
            data = {
                total: data.length,
                rows: data
            }
        }
        var dg = $(this);
        var opts = dg.datagrid('options');
        var pager = dg.datagrid('getPager');
        pager.pagination({
            onSelectPage: function (pageNum, pageSize) {
                opts.pageNumber = pageNum;
                opts.pageSize = pageSize;
                pager.pagination('refresh', {
                    pageNumber: pageNum,
                    pageSize: pageSize
                });
                dg.datagrid('loadData', data);
            }
        });
        if (!data.originalRows) {
            data.originalRows = (data.rows);
        }
        var start = (opts.pageNumber - 1) * parseInt(opts.pageSize);
        var end = start + parseInt(opts.pageSize);
        data.rows = (data.originalRows.slice(start, end));
        return data;
    }


    //点击查询按钮
    function selectRecordByName() {
        var departmentName = $("#departmentName").val();
        var randSession = "${authId}"
        window.location.href = 'getSys_DepartmentByLikeDepartmentName?departmentName=' + encodeURIComponent(departmentName) + "&randSession=" + randSession;
    }

    //点击添加部门信息按钮
    function addDepartment(num) {
        $('#addDialog' + num).dialog('open');  //打开对话框
        $('#addDialog' + num).dialog({modal: true});
    }


    function editDepartment(num) {
        var row = $("#dg1").datagrid('getSelected'); //获取选中行对象
        if (row != null) { //如果选中
            //为修改dialog框中的文本框赋值
            document.getElementById("editId" + num).value = row.id;
            $("#departmentName_edit" + num).val(row.departmentName);
            $("#description_edit" + num).val(row.description);
            if (num == 2) {
                $("#departmentId_edit" + num).val(row.departmentId);
            }
            $('#editDialog' + num).dialog('open');
            $('#editDialog' + num).dialog({modal: true});
        } else {
            $.messager.alert('操作提示', '请选择一条数据修改', 'info');
        }
    }


    function remove() {
        var row = $("#dg1").datagrid('getSelected'); //获取选中行对象
        var randSession = "${authId}"
        // alert(randSession);
        if (row != null) { //如果选中
            $.messager.alert('操作提示', '是否要将此部门删除？', 'warning', function (r) {
                $.ajax({
                    url: 'deleteSys_Department',
                    type: 'POST', //GET
                    async: true,    //或false,是否异步
                    contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
                    data: {"id": row.id, "randSession": randSession},
                    dataType: 'json',    //返回的数据格式：json/xml/html/script/jsonp/text
                    success: function (data) {
                        if (data.result == '1') {  //如果返回1，删除成功
                            $.messager.alert('', '删除部门成功', 'info', function (r) {
                                window.location.href = "sys_department";
                            });
                        } else {  //否则删除失败
                            $.messager.alert('操作提示', '此部门删除失败', 'error');
                        }
                    }
                })
            });
        } else {
            $.messager.alert('操作提示', '请选择一条数据删除', 'info');
        }
    }


    function openExcelTemplet() {
        $('#selectExcelTemplet').dialog('open');
        $('#selectExcelTemplet').dialog({modal: true});
    }


    function exportExcel(excelType) {
        var departmentName = $("#departmentName").val();
        var departmentId = $("#departmentId").val();
        var description = $("#description").val();
        var randSession = "${authId}"
        $.messager.confirm('操作提示', '确认将数据导出到Excel表格吗？', function (r) {
            if (r) {
                window.location.href = "exportSys_Department?excelType=" + excelType + "&departmentName=" + encode(departmentName) +
                    "&departmentId=" + departmentId + "&description=" + encode(description) + "&randSession=" + randSession;
                $('#selectExcelTemplet').dialog('close');
            }
        });
    }

    /************************************************************************************************************************************/
    /************************************************************************************************************************************/
    /************************************************************************************************************************************/


    function resetForAdd(num) {
        if (num == 2) {
            $("#departmentId_add" + num).val('');
        }
        $("#departmentName_add" + num).val('');
        $("#description_add" + num).val('');
    }

    function resetForEdit(num) {
        $("#departmentName_edit" + num).val('');
        $("#description_edit" + num).val('');
        if (num == 2) {
            $("#departmentId_edit" + num).val('');
        }

    }


    //点击新增dialog上的保存按钮
    function saveForAddDialog(num) {
        var departmentName_add = $("#departmentName_add" + num).val();
        var description_add = $("#description_add" + num).val();
        if (num == 2) {
            var departmentId_add = $("#departmentId_add" + num).val();
            if (description_add.length == 0) {
                $.messager.alert('操作提示', '分行名称不可为空', 'error');
                return;
            }
            if (departmentId_add.length == 0) {
                $.messager.alert('操作提示', '分行号不可为空', 'error');
                return;
            }
            if (departmentName_add.length > 255) {
                $.messager.alert('操作提示', '部门名称不得超过255', 'error');
                return;
            } else if (description_add.length > 255) {
                $.messager.alert('操作提示', '分行名称不得超过255', 'error');
                return;
            } else if (departmentId_add.length != 7) {
                $.messager.alert('操作提示', '分行号必须为7位', 'error');
                return;
            }
        } else {
            if (departmentName_add.length > 255 && description_add.length > 255) {
                $.messager.alert('操作提示', '部门名称和描述长度不得超过255', 'error');
                return;
            } else if (departmentName_add.length > 255) {
                $.messager.alert('操作提示', '部门名称不得超过255', 'error');
                return;
            } else if (description_add.length > 255) {
                $.messager.alert('操作提示', '描述不得超过255', 'error');
                return;
            }
            if (departmentName_add.length == 0) {
                $.messager.alert('操作提示', '部门名称不可为空', 'error');
                return;
            }
        }
        $("#saveButtonId" + num).linkbutton({disabled: true});
        $("#formForAddDepartment" + num).form('submit', {
            url: 'addSys_Department',
            onSubmit: function () {
                if ($("#formForAddDepartment" + num).form('validate')) {
                    $.messager.progress({
                        title: '请稍等',
                        msg: '正在保存部门信息......'
                    });
                    return true;
                } else {
                    $("#saveButtonId" + num).linkbutton({disabled: false});
                    return false;
                }
            },
            success: function (res) {
                $("#saveButtonId" + num).linkbutton({disabled: false});
                $.messager.progress('close');
                if (res == '1') {
                    $.messager.alert('', '新增成功！', 'info', function (r) {
                        window.location.href = "sys_department";
                    });
                } else if (res == '0') {
                    $.messager.alert('操作提示', '新增部门失败了', 'error');
                } else if (res == '2') {
                    $.messager.alert('操作提示', '部门名称已存在', 'error');
                } else if (res == '4') {
                    $.messager.alert('操作提示', '分行号和部门名称已存在', 'error');
                } else if (res == '5') {
                    $.messager.alert('操作提示', '分行名称和部门名称已存在', 'error');
                }
            },
            error: function () {
                $.messager.progress('close');
            }
        });
    }

    //点击修改dialog上的保存按钮
    function saveForEditDialog(num) {
        var departmentName_edit = $("#departmentName_edit" + num).val();
        var description_edit = $("#description_edit" + num).val();
        if (num == 2) {
            var departmentId_edit = $("#departmentId_edit" + num).val();
            if (description_edit.length == 0) {
                $.messager.alert('操作提示', '分行名称不可为空', 'error');
                return;
            }
            if (departmentId_edit.length == 0) {
                $.messager.alert('操作提示', '分行号不可为空', 'error');
                return;
            }
            if (departmentName_edit.length > 255) {
                $.messager.alert('操作提示', '部门名称不得超过255', 'error');
                return;
            } else if (description_edit.length > 255) {
                $.messager.alert('操作提示', '分行名称不得超过255', 'error');
                return;
            } else if (departmentId_edit.length != 7) {
                $.messager.alert('操作提示', '分行号必须为7位', 'error');
                return;
            }
        } else {

            if (departmentName_edit.length > 255 && description_edit.length > 255) {
                $.messager.alert('操作提示', '部门名称和描述长度不得超过255', 'error');
                return;
            } else if (departmentName_edit.length > 255) {
                $.messager.alert('操作提示', '部门名称不得超过255', 'error');
                return;
            } else if (description_edit.length > 255) {
                $.messager.alert('操作提示', '描述不得超过255', 'error');
                return;
            }
            if (departmentName_edit.length == 0) {
                $.messager.alert('操作提示', '部门名称不可为空', 'error');
                return;
            }
        }

        $("#editButtonId" + num).linkbutton({disabled: true});
        $("#formForEditDepartment" + num).form('submit', {
            url: 'editSys_Department',
            onSubmit: function () {
                if ($("#formForEditDepartment" + num).form('validate')) {
                    $.messager.progress({
                        title: '请稍等',
                        msg: '正在保存部门信息......'
                    });
                    return true;
                } else {
                    $("#editButtonId" + num).linkbutton({disabled: false});
                    return false;
                }
            },
            success: function (res) {
                $.messager.progress('close');
                if (res == '1') {
                    $.messager.alert('', '修改成功！', 'info', function (r) {
                        window.location.href = "sys_department";
                    });
                } else if (res == '0') {
                    $.messager.alert('操作提示', '修改部门失败了', 'error');
                } else if (res == '3') {
                    $.messager.alert('操作提示', '部门名称或描述过长', 'error');
                } else if (res == '2') {
                    $.messager.alert('操作提示', '部门名称已存在', 'error');
                } else if (res == '4') {
                    $.messager.alert('操作提示', '分行号和部门名称已存在', 'error');
                } else if (res == '5') {
                    $.messager.alert('操作提示', '分行名称和部门名称已存在', 'error');
                } else {
                    $.messager.alert('操作提示', '系统异常', 'error')
                }
                $("#editButtonId" + num).linkbutton({disabled: false});
            },
            error: function () {
                $.messager.progress('close');
                $("#editButtonId" + num).linkbutton({disabled: false});
            }
        });
    }


</script>
</body>
</html>
